package com.yuanhua.xcxmng.wechat.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.yuanhua.xcxmng.wechat.cache.LocalCache;
import com.yuanhua.xcxmng.wechat.cons.ErrorCode;
import com.yuanhua.xcxmng.wechat.entity.User;
import com.yuanhua.xcxmng.wechat.exception.SessionException;
import com.yuanhua.xcxmng.wechat.service.MyService;
import com.yuanhua.xcxmng.wechat.utils.ApiRequestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

public abstract class BaseController {
    protected String  SESSION_ID_KEY = "token";
    @Autowired
    private MyService myService;

    protected Log     logger         = LogFactory.get();

    protected String getOpenId() throws SessionException {
        HttpServletRequest request = ApiRequestUtils.getHttpServletRequest();
        String threeSession = StringUtils.EMPTY;

        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            if (StringUtils.equalsIgnoreCase(key, SESSION_ID_KEY)) {
                // 获取sessionKey
                threeSession = request.getHeader(SESSION_ID_KEY);

                String sessionCode = LocalCache.getInStance().getLocalCache(threeSession);
                if (StringUtils.isBlank(sessionCode)) {
                    throw new SessionException(ErrorCode.SESSION_ID_INVALID, threeSession);
                }
                String openId = StringUtils.split(sessionCode, "^#^")[1];
                return openId;
            }
        }
        throw new SessionException(ErrorCode.SESSION_ID_INVALID, threeSession);
    }

    protected String getSeesionKey() throws SessionException {
        HttpServletRequest request = ApiRequestUtils.getHttpServletRequest();
        String threeSession = StringUtils.EMPTY;

        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            if (StringUtils.equalsIgnoreCase(key, SESSION_ID_KEY)) {
                // 获取sessionKey
                threeSession = request.getHeader(SESSION_ID_KEY);

                String sessionCode = LocalCache.getInStance().getLocalCache(threeSession);
                if (StringUtils.isBlank(sessionCode)) {
                    throw new SessionException(ErrorCode.SESSION_ID_INVALID, threeSession);
                }
                String sessionKey = sessionCode.split("^#^")[0];
                return sessionKey;
            }
        }
        throw new SessionException(ErrorCode.SESSION_ID_INVALID, threeSession);
    }

    protected User getUser() throws SessionException {
        User user = null;
        String openId = getOpenId();
        try {
            user = myService.findUserByOpenId(openId);
        } catch (Exception e) {
            logger.error(e, "获取用失败, openId:{}", openId);
        }
        return user;
    }
}
